home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / andere sprachen / oberonv4 / oberon-src / system / textframes.mod (.txt) < prev    next >
Encoding:
Oberon Text  |  1996-02-19  |  59.5 KB  |  1,279 lines

  1. Syntax24.Scn.Fnt
  2. Syntax10.Scn.Fnt
  3. Syntax10i.Scn.Fnt
  4. Syntax10b.Scn.Fnt
  5. Syntax8.Scn.Fnt
  6. Syntax12.Scn.Fnt
  7. (* Notify Ralf for maintenance of Non-FPU source *)
  8. MODULE TextFrames;    (** CAS/MH/HM 20.4.94/NW 12.11.94 **)    (*<< Linz Cursor Up/Down*)
  9.     IMPORT Modules, Input, Display, Files, Fonts, Viewers, Oberon, MenuViewers, Texts, Pictures;
  10.     CONST
  11.         (** update message IDs **)
  12.             replace* = 0; insert* = 1; delete* = 2;
  13.         (** units **)
  14.             mm* = 36000;  Unit* = 10000;
  15.         (** parc options **)
  16.             gridAdj* = 0;  leftAdj* = 1;  rightAdj* = 2;  pageBreak* = 3;    twoColumns* = 4;
  17.         (** maximum number of TAB stops in Parc **)
  18.             MaxTabs* = 32;
  19.         AdjMask = {leftAdj, rightAdj};
  20.         TAB = 9X; LF = 0AX; CR = 0DX; DEL = 7FX; BRK = 0ACX; ShiftBRK = 0ADX; CRSL = 0C4X; CRSR = 0C3X;
  21.         CRSU = 0C1X; CRSD = 0C2X;    (*<<*)
  22.         AdjustSpan = 30; MinTabWidth = 3 * mm; StdTabWidth = 4 * mm;
  23.         rightKey = 0; middleKey = 1; leftKey = 2; cancel = {rightKey, middleKey, leftKey};
  24.     TYPE
  25.         Parc* = POINTER TO ParcDesc;
  26.         ParcDesc* = RECORD (Texts.ElemDesc)
  27.             left*: LONGINT;    (** distance from (F.X + F.left); in units **)
  28.             first*: LONGINT;    (** first line indentation from P.left; in units **)
  29.             width*: LONGINT;    (** parc width; in units **)
  30.             lead*: LONGINT;    (** distance to previous line; in units **)
  31.             lsp*: LONGINT;    (** line spacing of text after P; in units **)
  32.             dsr*: LONGINT;    (** descender of text after P; in units **)
  33.             opts*: SET;
  34.             nofTabs*: INTEGER;
  35.             tab*: ARRAY MaxTabs OF LONGINT    (** in units **)
  36.         END;
  37.         TextLine = POINTER TO TextLineDesc;
  38.         Location* = RECORD
  39.             org*, pos*: LONGINT;
  40.             x*, y*, dx*, dy*: INTEGER;
  41.             line: TextLine
  42.         END;
  43.         TextLineDesc = RECORD
  44.             next: TextLine;
  45.             eot: BOOLEAN;                (* contains end of text *)
  46.             indent: LONGINT;                (* line indentation in units *)
  47.             w, h, dsr: INTEGER;            (* bounding box clipped to frame (w including indent) *)
  48.             w0, nob: INTEGER;            (* unclipped width (including indent), number of contained blanks: nob > 0 if text line wraps around *)
  49.             org, len, span: LONGINT;    (* len ... characters w/o; span ... w/ trailing CR or white space, if any *)
  50.             P: Parc;                            (* last parc before this text line *)
  51.             pbeg: LONGINT                (* position of P *)
  52.         END;
  53.         Frame* = POINTER TO FrameDesc;
  54.         FrameDesc* = RECORD (Display.FrameDesc)
  55.             text*: Texts.Text;
  56.             org*: LONGINT;
  57.             col*, left*, right*, top*, bot*: INTEGER;
  58.             markH*: INTEGER;    (** position of tick mark in scroll bar (< 0 => no tick mark) **)
  59.             barW*: INTEGER;    (** scroll bar width **)
  60.             time*: LONGINT;    (** selection time **)
  61.             hasCar*, hasSel*, showsParcs*: BOOLEAN;    (** caret/selection present; parcs visible **)
  62.             carloc*, selbeg*, selend*: Location;
  63.             focus*: Display.Frame;    (** frame of nested element if this element contains the focus **)
  64.             trailer: TextLine    (* ring with trailer and header *)
  65.         END;
  66.         DisplayMsg* = RECORD (Texts.ElemMsg)
  67.             prepare*: BOOLEAN;
  68.             fnt*: Fonts.Font;
  69.             col*: SHORTINT;
  70.             pos*: LONGINT;    (** position in host text **)
  71.             frame*: Display.Frame;    (** ~prepare => host frame **)
  72.             X0*, Y0*: INTEGER;    (** ~prepare => receiver origin in screen space **)
  73.             indent*: LONGINT;    (** prepare => width already consumed in line, in units **)
  74.             elemFrame*: Display.Frame    (** optional return parameter **)
  75.         END;
  76.         TrackMsg* = RECORD (Texts.ElemMsg)
  77.             X*, Y*: INTEGER;
  78.             keys*: SET;
  79.             fnt*: Fonts.Font;
  80.             col*: SHORTINT;
  81.             pos*: LONGINT;    (** position in host text **)
  82.             frame*: Display.Frame;    (** host frame **)
  83.             X0*, Y0*: INTEGER    (** receiver origin in screen space **)
  84.         END;
  85.         FocusMsg* = RECORD (Texts.ElemMsg)
  86.             focus*: BOOLEAN;    (** whether to focus or to defocus **)
  87.             elemFrame*: Display.Frame;    (** focus/defocus target **)
  88.             frame*: Display.Frame    (** host frame **)
  89.         END;
  90.         NotifyMsg* = RECORD (Display.FrameMsg)
  91.             frame*: Display.Frame    (** host frame **)
  92.         END;
  93.         UpdateMsg* = RECORD (Display.FrameMsg)
  94.             id*: INTEGER;
  95.             text*: Texts.Text;
  96.             beg*, end*: LONGINT
  97.         END;
  98.         InsertElemMsg* = RECORD (Display.FrameMsg)
  99.             e*: Texts.Elem
  100.         END;
  101.         SelectMsg = RECORD (Display.FrameMsg)
  102.             text: Texts.Text;
  103.             beg, end: LONGINT;
  104.             time: LONGINT
  105.         END;
  106.         menuH*, barW*, left*, right*, top*, bot*: INTEGER;
  107.         defParc*: Parc;
  108.         (*shared globals => get rid off in a later version?*)
  109.         W, W0: Texts.Writer;
  110.         B: Texts.Buffer;
  111.         P: Parc;
  112.         pbeg: LONGINT;    (*inv T[pbeg] = P*)
  113.         R: Texts.Reader;
  114.         nextCh: CHAR;    (*inv Base(R) = T => T[Pos(R)-1] = nextCh]*)
  115.         par: Oberon.ParList;
  116.         neutralize: Oberon.ControlMsg;
  117.     PROCEDURE Min (x, y: INTEGER): INTEGER;
  118.     BEGIN IF x < y THEN RETURN x ELSE RETURN y END
  119.     END Min;
  120.     PROCEDURE Max (x, y: INTEGER): INTEGER;
  121.     BEGIN IF x > y THEN RETURN x ELSE RETURN y END
  122.     END Max;
  123.     PROCEDURE MarkMenu (F: Frame);
  124.         VAR R: Texts.Reader; V: Viewers.Viewer; T: Texts.Text; ch: CHAR;
  125.     BEGIN V := Viewers.This(F.X, F.Y);
  126.         IF (V IS MenuViewers.Viewer) & (V.dsc IS Frame) & (F # V.dsc) THEN
  127.             T := V.dsc(Frame).text;
  128.             IF T.len > 0 THEN Texts.OpenReader(R, T, T.len - 1); Texts.Read(R, ch) ELSE ch := 0X END;
  129.             IF ch # "!" THEN Texts.Write(W0, "!"); Texts.Append(T, W0.buf) END
  130.         END
  131.     END MarkMenu;
  132.     (* Element Subframes *)
  133.     PROCEDURE InvertBorder (F: Display.Frame);
  134.     BEGIN
  135.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y-1, F.W+2, 1, Display.invert);
  136.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y+F.H, F.W+2, 1, Display.invert);
  137.         Display.ReplPattern(Display.white, Display.grey1, F.X-1, F.Y, 1, F.H, Display.invert);
  138.         Display.ReplPattern(Display.white, Display.grey1, F.X+F.W, F.Y, 1, F.H, Display.invert)
  139.     END InvertBorder;
  140.     PROCEDURE InvalSubFrames (F: Frame; x, y, w, h: INTEGER);    (* removes and suspends all subframes partly in (x, y, w, h) *)
  141.         VAR p, f: Display.Frame; msg: MenuViewers.ModifyMsg;
  142.     BEGIN
  143.         IF (w > 0) & (h > 0) THEN f := F.dsc;
  144.             IF f # NIL THEN p := f; f := p.next END;
  145.             WHILE f # NIL DO
  146.                 IF (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN p.next := f.next;
  147.                     msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  148.                     f.handle(f, msg)
  149.                 ELSE p := f
  150.                 END;
  151.                 f := p.next
  152.             END;
  153.             f := F.dsc;
  154.             IF (f # NIL) & (f.X < x + w) & (f.X + f.W > x) & (f.Y < y + h) & (f.Y + f.H > y) THEN F.dsc := F.dsc.next;
  155.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := 0;
  156.                 f.handle(f, msg)
  157.             END
  158.         END
  159.     END InvalSubFrames;
  160.     PROCEDURE ShiftSubFrames (F: Frame; oldY, newY, h: INTEGER);    (* shift (F.X, oldY, F.W, h) to (F.X, newY, F.W, h) *)
  161.         VAR f: Display.Frame; msg: MenuViewers.ModifyMsg;
  162.     BEGIN
  163.         IF oldY > newY THEN InvalSubFrames(F, F.X, newY, F.W, oldY - newY)
  164.         ELSE InvalSubFrames(F, F.X, oldY + h, F.W, newY - oldY)
  165.         END;
  166.         f := F.dsc;
  167.         WHILE f # NIL DO
  168.             IF (f.Y < oldY + h) & (f.Y + f.H > oldY) THEN INC(f.Y, newY - oldY);
  169.                 msg.id := MenuViewers.reduce; msg.dY := 0; msg.Y := f.Y; msg.H := f.H;
  170.                 f.handle(f, msg)
  171.             END;
  172.             f := f.next
  173.         END
  174.     END ShiftSubFrames;
  175.     PROCEDURE NotifySubFrames (F: Frame; VAR msg: Display.FrameMsg);
  176.         VAR p, f: Display.Frame;
  177.     BEGIN f := F.dsc;
  178.         IF msg IS NotifyMsg THEN msg(NotifyMsg).frame := F END;
  179.         WHILE f # NIL DO p := f; f := f.next; p.handle(p, msg) END
  180.     END NotifySubFrames;
  181.     (* Display Primitives *)
  182.     PROCEDURE DrawCursor (x, y: INTEGER);
  183.     BEGIN Oberon.DrawCursor(Oberon.Mouse, Oberon.Arrow, x, y)
  184.     END DrawCursor;
  185.     PROCEDURE TrackMouse (VAR x, y: INTEGER; VAR keys, keysum: SET);
  186.     BEGIN Input.Mouse(keys, x, y); keysum := keysum + keys; DrawCursor(x, y)
  187.     END TrackMouse;
  188.     PROCEDURE EraseRect (F: Frame; x, y, w, h: INTEGER);
  189.     BEGIN Display.ReplConst(F.col, x, y, w, h, Display.replace); InvalSubFrames(F, x, y, w, h)
  190.     END EraseRect;
  191.     PROCEDURE Erase (F: Frame; x, y, w, h: INTEGER);    (*RemoveMarks optimization*)
  192.     BEGIN
  193.         IF h > 0 THEN Oberon.RemoveMarks(x, y, w, h); EraseRect(F, x, y, w, h) END
  194.     END Erase;
  195.     PROCEDURE Shift (F: Frame; oldY, newY, h: INTEGER);    (*RemoveMarks optimization*)
  196.     BEGIN
  197.         IF (oldY # newY) & (h > 0) THEN
  198.             Oberon.RemoveMarks(F.X + F.left, Min(oldY, newY), F.W - F.left, Max(oldY, newY) + h);
  199.             Display.CopyBlock(F.X + F.left, oldY, F.W - F.left, h, F.X + F.left, newY, Display.replace);
  200.             ShiftSubFrames(F, oldY, newY, h)
  201.         END
  202.     END Shift;
  203.     PROCEDURE InvertCaret (F: Frame);
  204.         VAR loc: Location; bot: INTEGER;
  205.     BEGIN loc := F.carloc; bot := loc.y + loc.line.dsr - 6;
  206.         Display.CopyPatternC(F, Display.white, Display.hook, loc.x, bot, Display.invert)
  207.     END InvertCaret;
  208.     PROCEDURE InvertRect (F: Frame; x, y, w, h: INTEGER);    (*clips to right and bottom frame margin*)
  209.     BEGIN
  210.         IF x + w > F.X + F.W - F.right THEN w := F.X + F.W - F.right - x END;
  211.         IF y >= F.Y + F.bot THEN Display.ReplConst(Display.white, x, y, w, h, Display.invert) END
  212.     END InvertRect;
  213.     PROCEDURE InvertSelection (F: Frame; beg, end: Location);
  214.         VAR t: TextLine; ex, rx, w, py: INTEGER;
  215.     BEGIN
  216.         rx := F.X + F.W - F.right; t := end.line;
  217.         IF t.eot OR (end.pos <= t.org + t.len) THEN ex := end.x ELSE ex := rx END;
  218.         IF beg.line = end.line THEN InvertRect(F, beg.x, beg.y, ex - beg.x, beg.line.h)
  219.         ELSE t := beg.line; py := beg.y; w := F.W - F.left - F.right;
  220.             InvertRect(F, beg.x, py, rx - beg.x, t.h); t := t.next; DEC(py, t.h);
  221.             WHILE t # end.line DO InvertRect(F, F.X + F.left, py, w, t.h); t := t.next; DEC(py, t.h) END;
  222.             InvertRect(F, F.X + F.left, py, ex - (F.X + F.left), t.h)
  223.         END
  224.     END InvertSelection;
  225.     PROCEDURE CoordToPos (F: Frame; mh: INTEGER): LONGINT;
  226.         VAR h: INTEGER;
  227.     BEGIN h := F.H - 1;
  228.         IF h > 0 THEN RETURN ENTIER(F.text.len / h * (h - mh)) ELSE RETURN 0 END
  229.     END CoordToPos;
  230.     PROCEDURE ShowBar (F: Frame; botH, topH: INTEGER);
  231.     BEGIN
  232.         IF (F.left > F.barW) & (F.barW > 0) THEN
  233.             Display.ReplConst(Display.white, F.X + F.barW - 1, F.Y + botH, 1, topH - botH, Display.replace)
  234.         END
  235.     END ShowBar;
  236.     PROCEDURE Tick (F: Frame);
  237.     BEGIN
  238.         IF (0 <= F.markH) & (F.markH < F.H) & (F.left > F.barW) & (F.barW > 6) & (F.H > 2) THEN
  239.             Display.ReplConst(Display.white, F.X + 1, F.Y + F.markH, F.barW - 6, 2, Display.invert)
  240.         END
  241.     END Tick;
  242.     PROCEDURE ShowTick (F: Frame);    (* removes global marks as needed *)
  243.         VAR h, mh: INTEGER; len: LONGINT;
  244.     BEGIN
  245.         h := F.H - 2; len := F.text.len;
  246.         IF len > 0 THEN mh := SHORT(ENTIER(h - F.org / len * h)) ELSE mh := h END;
  247.         IF F.markH # mh THEN Oberon.RemoveMarks(F.X, F.Y, F.barW, F.H);
  248.             Tick(F); F.markH := mh; Tick(F)
  249.         END
  250.     END ShowTick;
  251.     PROCEDURE Mark* (F: Frame; mark: INTEGER);
  252.     BEGIN
  253.         Erase(F, F.X, F.Y, F.barW - 1, F.H); F.markH := -1;
  254.         IF (mark < 0) & (F.H >= 16) THEN
  255.             Display.CopyPattern(Display.white, Display.downArrow, F.X, F.Y, Display.invert)
  256.         ELSIF mark > 0 THEN
  257.             ShowTick(F)
  258.         END
  259.     END Mark;
  260.     (** Parcs **)
  261.     PROCEDURE ParcBefore* (T: Texts.Text; pos: LONGINT; VAR P: Parc; VAR beg: LONGINT);
  262.         VAR R: Texts.Reader;
  263.     BEGIN Texts.OpenReader(R, T, pos + 1);
  264.         REPEAT Texts.ReadPrevElem(R) UNTIL R.eot OR (R.elem IS Parc);
  265.         IF R.eot THEN P := defParc; beg := -1 ELSE P := R.elem(Parc); beg := Texts.Pos(R) END
  266.     END ParcBefore;
  267.     PROCEDURE InitDefParc;
  268.     BEGIN
  269.         IF Modules.ThisMod("ParcElems") = NIL THEN HALT(99) END
  270.         (* side effect: body of ParcElems initialises defParc *)
  271.     END InitDefParc;
  272.     (* Screen Metrics *)
  273.     PROCEDURE Tab (dw: INTEGER; VAR dx: INTEGER);    (*P set*)
  274.         (* dw = line width from left margin to caret (in pixels); dx = distance from caret to next tab stop (in pixels) *)
  275.         VAR i, n: INTEGER; w: LONGINT;
  276.     BEGIN
  277.         i := 0; n := P.nofTabs; w := LONG(dw) * Unit + MinTabWidth;
  278.         IF dw < 0 THEN dx := -dw
  279.         ELSE
  280.             WHILE (i < n) & (P.tab[i] < w) DO INC(i) END;
  281.             IF i < n THEN dx := SHORT((P.tab[i] - LONG(dw) * Unit) DIV Unit)
  282.             ELSE dx := StdTabWidth DIV Unit
  283.             END
  284.         END
  285.     END Tab;
  286.     PROCEDURE MeasureSpecial (dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  287.         (* returns metrics of nextCh (nextCh <= " "); sends prepare message to elements; P, R, nextCh set *)
  288.         VAR e: Texts.Elem; pat: Display.Pattern; msg: DisplayMsg;
  289.     BEGIN
  290.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  291.             x := 0; y := 0; w := dx; h := 0
  292.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  293.         ELSIF R.elem # NIL THEN e := R.elem;
  294.             msg.prepare := TRUE; msg.indent := LONG(dw) * Unit;
  295.             msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R)-1;
  296.             msg.Y0 := -SHORT(P.dsr DIV Unit);    (*<<< 18-Nov-91*)
  297.             e.handle(e, msg);
  298.             w := SHORT(e.W DIV Unit);
  299.             dx := w; x := 0; y := msg.Y0; h := SHORT(e.H DIV Unit)    (*<<< 18-Nov-91*)
  300.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  301.         END
  302.     END MeasureSpecial;
  303.     PROCEDURE GetSpecial (F: Frame; VAR n: INTEGER; cn, ddx, dw: INTEGER; VAR dx, x, y, w, h: INTEGER);
  304.         (* returns metrics of nextCh (nextCh <= " "); no prepare message to elements; extends blanks for block adjust *)
  305.         (* cn ... add 1 pixel to first cn blanks (block adjust); ddx ... add ddx pixels to every blank (block adjust) *)
  306.         (*P, R, nextCh set*)
  307.         VAR e: Texts.Elem; pat: Display.Pattern;
  308.     BEGIN
  309.         IF nextCh = " " THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  310.             x := 0; y := 0; INC(dx, ddx); INC(n); IF n <= cn THEN INC(dx) END;    (*space correction for block adjustment*)
  311.             w := dx; h := 0
  312.         ELSIF nextCh = TAB THEN Tab(dw, dx); x := 0; y := 0; w := dx; h := 0
  313.         ELSIF R.elem # NIL THEN e := R.elem;
  314.             IF (e IS Parc) & (P.W = 9999 * Unit) THEN (* P gets this value in prepare message *)
  315.                 w := Min(SHORT((P.width + P.left) DIV Unit), F.W - F.right - F.left);
  316.                 e.W := LONG(w) * Unit
  317.             ELSE w := SHORT(e.W DIV Unit)
  318.             END;
  319.             dx := w; x := 0; y := -SHORT(P.dsr DIV Unit); h := SHORT(e.H DIV Unit)
  320.         ELSIF ~R.eot THEN Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  321.         ELSE dx := 0; x := 0; y := 0; w := 0; h := 0
  322.         END
  323.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  324.         END
  325.     END GetSpecial;
  326.     PROCEDURE NextLine (T: Texts.Text; VAR org: LONGINT);    (*R, nextCh set; org = Texts.Pos(R)-1*)
  327.         VAR pat: Display.Pattern; pos, bk, d: LONGINT; width, tw, dx, x, y, w, h: INTEGER;
  328.             R1: Texts.Reader; peekCh: CHAR; indent: INTEGER;
  329.     BEGIN
  330.         tw := 0; dx := 0; w := 0; bk := -999;    (* bk = pos of last seperator *)
  331.         pos := org; ParcBefore(T, pos, P, pbeg); width := SHORT(P.width DIV Unit);
  332.         indent := 0;
  333.         IF org > 0 THEN Texts.OpenReader(R1, T, org - 1); Texts.Read(R1, peekCh);
  334.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN indent := SHORT(P.first DIV Unit) END
  335.         END;
  336.         INC(tw, indent);
  337.         LOOP INC(pos);    (*inv pos = Texts.Pos(R), ~R.eof => nextCh = text[pos-1]*)
  338.             IF R.eot OR (nextCh = CR) THEN EXIT END;
  339.             INC(tw, dx);
  340.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  341.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  342.             END;
  343.             IF tw + x + w > width THEN d := pos - bk;
  344.                 IF (d < AdjustSpan) & (nextCh > " ") THEN pos := bk
  345.                 ELSIF ((nextCh > " ") OR (nextCh = Texts.ElemChar)) & (pos > org + 1) THEN DEC(pos)
  346.                 END;
  347.                 Texts.OpenReader(R, T, pos); Texts.Read(R, nextCh);
  348.                 EXIT
  349.             END;
  350.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN bk := pos END;
  351.             Texts.Read(R, nextCh)
  352.         END;
  353.         org := pos
  354.     END NextLine;
  355.     PROCEDURE BegOfLine (T: Texts.Text; VAR pos: LONGINT; adjust: BOOLEAN);
  356.         (* returns origin of line containing pos *)
  357.         VAR p, org: LONGINT;
  358.     BEGIN
  359.         IF pos <= 0 THEN pos := 0
  360.         ELSE
  361.             IF pos <= T.len THEN org := pos ELSE org := T.len END;
  362.             LOOP    (*search backwards for CR*)
  363.                 IF org = 0 THEN EXIT END;
  364.                 Texts.OpenReader(R, T, org - 1); Texts.Read(R, nextCh);
  365.                 IF nextCh = CR THEN EXIT END;
  366.                 DEC(org)
  367.             END;
  368.             IF adjust THEN    (*search forward for actual line origin*)
  369.                 Texts.OpenReader(R, T, org); Texts.Read(R, nextCh); p := org;
  370.                 REPEAT org := p; NextLine(T, p) UNTIL (p > pos) OR R.eot
  371.             END;
  372.             pos := org
  373.         END
  374.     END BegOfLine;
  375.     PROCEDURE AdjustMetrics (F: Frame; t: TextLine; VAR pw, tw, ddx, cn: INTEGER);    (*t.org set*)
  376.         (* pw ... x-coord of first char in line (in pixels); tw ... width of text line; ddx, cn ... see GetSpecial *)
  377.     BEGIN
  378.         P := t.P; pbeg := t.pbeg;
  379.         pw := F.left; tw := t.w; ddx := 0; cn := 0;
  380.         IF t.pbeg # t.org THEN
  381.             INC(pw, SHORT((P.left + t.indent) DIV Unit));
  382.             IF leftAdj IN P.opts THEN
  383.                 IF (rightAdj IN P.opts) & (t.nob > 0) THEN
  384.                     tw := SHORT(P.width DIV Unit); ddx := (tw - t.w0) DIV t.nob; cn := (tw - t.w0) MOD t.nob
  385.                 END
  386.             ELSIF rightAdj IN P.opts THEN INC(pw, SHORT(P.width DIV Unit) - t.w0)
  387.             ELSE (*center*) INC(pw, (SHORT(P.width DIV Unit) - t.w0) DIV 2)
  388.             END;
  389.             DEC(tw, SHORT(t.indent DIV Unit))
  390.         END
  391.     END AdjustMetrics;
  392.     (* Screen Placement *)
  393.     PROCEDURE DrawSpecial (F: Frame; px, py, x, y: INTEGER);    (*R, nextCh set*)
  394.         VAR e: Texts.Elem; pat: Display.Pattern; dx, w, h: INTEGER; msg: DisplayMsg;
  395.     BEGIN
  396.         IF (nextCh = TAB) OR (nextCh = CR) THEN (*skip*)
  397.         ELSIF R.elem # NIL THEN e := R.elem;
  398.             IF ~(e IS Parc) OR F.showsParcs THEN
  399.                 msg.prepare := FALSE; msg.fnt := R.fnt; msg.col := R.col; msg.pos := Texts.Pos(R) - 1;
  400.                 msg.frame := F; msg.X0 := px + x; msg.Y0 := py + y; msg.elemFrame := NIL;
  401.                 e.handle(e, msg);
  402.                 IF msg.elemFrame # NIL THEN msg.elemFrame.next := F.dsc; F.dsc := msg.elemFrame END
  403.             ELSIF pageBreak IN e(Parc).opts THEN (*(e IS Parc) & ~F.showsParcs*)
  404.                 Display.ReplPattern(Display.white, Display.grey1, px + x, py, SHORT(e.W DIV Unit), 1, Display.replace)
  405.             END
  406.         ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat);
  407.             Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  408.         END
  409.     END DrawSpecial;
  410.     PROCEDURE ShowLine (F: Frame; t: TextLine; left, right, py: INTEGER);
  411.         VAR pat: Display.Pattern; i: LONGINT; n, cn, lm, px, pw, tw, ddx, dx, x, y, w, h: INTEGER;
  412.     BEGIN
  413.         (* lm ... left parc margin in screen coord; pw ...  x of first char in frame coord *)
  414.         Texts.OpenReader(R, F.text, t.org); AdjustMetrics(F, t, pw, tw, ddx, cn);
  415.         lm := F.X + F.left + SHORT(P.left DIV Unit); px := F.X + pw; INC(py, t.dsr); i := 0; n := 0;
  416.         WHILE i < t.len DO Texts.Read(R, nextCh);
  417.             IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, px - lm, dx, x, y, w, h)
  418.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  419.             END;
  420.             INC(y, R.fnt.height * R.voff DIV 64);
  421.             IF px + x + w <= right THEN
  422.                 IF px + x >= left THEN
  423.                     IF nextCh <= " " THEN DrawSpecial(F, px, py, x, y)
  424.                     ELSE Display.CopyPattern(R.col, pat, px + x, py + y, Display.invert)
  425.                     END
  426.                 END;
  427.                 INC(px, dx); INC(i)
  428.             ELSE i := t.len
  429.             END
  430.         END
  431.     END ShowLine;
  432.     PROCEDURE ShowLines (F: Frame; botH, topH: INTEGER; erase: BOOLEAN);
  433.         VAR t: TextLine; ph: INTEGER;
  434.     BEGIN
  435.         t := F.trailer.next; ph := F.H - F.top;
  436.         WHILE (t # F.trailer) & (ph - t.h >= topH) DO DEC(ph, t.h); t := t.next END;
  437.         WHILE (t # F.trailer) & (ph - t.h >= botH) DO DEC(ph, t.h);
  438.             IF erase THEN Erase(F, F.X + F.left, F.Y + ph, F.W - F.right - F.left, t.h) END;
  439.             ShowLine(F, t, F.X + F.left, F.X + F.W - F.right, F.Y + ph); t := t.next
  440.         END
  441.     END ShowLines;
  442.     (* Screen Casting *)
  443.     PROCEDURE MeasureLine (F: Frame; maxW: INTEGER; t: TextLine);    (* R, nextCh set *)
  444.         VAR pat: Display.Pattern; len, bklen, d: LONGINT; eol: BOOLEAN;
  445.             nob, bknob, width, minY, bkminY, maxY, bkmaxY, tw, bktw, lsp, dsr, dx, x, y, w, h: INTEGER;
  446.             R1: Texts.Reader; peekCh: CHAR;
  447.             (* bk* ... backup for last blank *)
  448.     BEGIN
  449.         len := 0; nob := 0; bklen := -999; tw := 0; dx := 0; minY := 0; maxY := 0;
  450.         ParcBefore(F.text, t.org, P, pbeg);
  451.         lsp := SHORT(P.lsp DIV Unit); dsr := SHORT(P.dsr DIV Unit); width := SHORT(P.width DIV Unit);
  452.         t.indent := 0;
  453.         IF t.org > 0 THEN Texts.OpenReader(R1, F.text, t.org - 1); Texts.Read(R1, peekCh);
  454.             IF (peekCh = CR) OR (R1.elem # NIL) & (R1.elem IS Parc) THEN t.indent := P.first END
  455.         END;
  456.         INC(tw, SHORT(t.indent DIV Unit));
  457.         LOOP
  458.             IF R.eot OR (nextCh = CR) THEN nob := 0; eol := ~R.eot; EXIT END;
  459.             IF nextCh <= " " THEN MeasureSpecial(tw, dx, x, y, w, h)
  460.             ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  461.             END;
  462.             IF tw + x + w > width THEN d := len - bklen;
  463.                 IF (d < AdjustSpan) & (nextCh > " ") THEN eol := TRUE;
  464.                     Texts.OpenReader(R, F.text, Texts.Pos(R) - d);
  465.                     nob := bknob; len := bklen; tw := bktw; minY := bkminY; maxY := bkmaxY
  466.                 ELSIF len = 0 THEN    (* force at least one character on each line *)
  467.                     INC(len); INC(y, R.fnt.height * R.voff DIV 64); minY := Min(minY, y); maxY := Max(maxY, y + h);
  468.                     Texts.Read(R, nextCh); eol := FALSE; tw := maxW
  469.                 ELSE eol := (nextCh <= " ") & (nextCh # Texts.ElemChar)
  470.                 END;
  471.                 EXIT
  472.             END;
  473.             IF (nextCh <= " ") & (nextCh # Texts.ElemChar) THEN
  474.                 bknob := nob; bklen := len; bktw := tw; bkminY := minY; bkmaxY := maxY;
  475.                 IF nextCh = " " THEN INC(nob) END
  476.             END;
  477.             INC(len); INC(tw, dx); INC(y, R.fnt.height * R.voff DIV 64);
  478.             IF y < minY THEN minY := y END;
  479.             IF y + h > maxY THEN maxY := y + h END;
  480.             Texts.Read(R, nextCh)
  481.         END;
  482.         IF ~F.showsParcs & (pbeg = t.org) THEN dsr := 0; t.h := SHORT(P.lead DIV Unit) + 1
  483.         ELSIF gridAdj IN P.opts THEN
  484.             WHILE dsr < -minY DO INC(dsr, lsp) END;
  485.             t.h := Max(lsp, dsr + maxY); INC(t.h, (-t.h) MOD lsp)
  486.         ELSE dsr := Max(dsr, -minY); t.h := Max(lsp, dsr + maxY)
  487.         END;
  488.         t.len := len; t.w0 := tw; t.w := Min(tw, maxW); t.dsr := dsr; t.nob := nob; t.eot := R.eot; t.P := P; t.pbeg := pbeg;
  489.         IF eol THEN Texts.Read(R, nextCh); t.span := len + 1 ELSE t.span := len END
  490.     END MeasureLine;
  491.     PROCEDURE MeasureLines (F: Frame; org: LONGINT; VAR trailer: TextLine);
  492.         VAR s, t: TextLine; ph: INTEGER;
  493.     BEGIN
  494.         NEW(trailer); s := trailer;
  495.         Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); ph := F.H - F.top;
  496.         LOOP NEW(t); t.org := org; MeasureLine(F, F.W - F.left - F.right, t);
  497.             IF ph - t.h < F.bot THEN EXIT END;
  498.             s.next := t; s := t; INC(org, s.span); DEC(ph, s.h);
  499.             IF R.eot THEN EXIT END
  500.         END;
  501.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; (* start of first invisible line *) trailer.len := 0; trailer.w := 0;
  502.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P (* P set by MeasureLine *) ; trailer.pbeg := pbeg
  503.     END MeasureLines;
  504.     (** Locators **)
  505.     PROCEDURE LocateLineTop (F: Frame; trailer: TextLine; org: LONGINT; VAR loc: Location);
  506.         VAR t: TextLine; ph: INTEGER;
  507.     BEGIN
  508.         ph := F.H - F.top; t := trailer.next;
  509.         WHILE (t # trailer) & (t.org # org) DO DEC(ph, t.h); t := t.next END;
  510.         loc.org := org; loc.line := t; loc.y := F.Y + ph
  511.     END LocateLineTop;
  512.     PROCEDURE Width (F: Frame; t: TextLine; pos: LONGINT; VAR pw, dx, dy: INTEGER);
  513.         VAR pat: Display.Pattern; i: LONGINT; n, mw, lm, tw, ddx, cn, x, y, w, h: INTEGER;
  514.     BEGIN
  515.         AdjustMetrics(F, t, pw, tw, ddx, cn); dy := 0; lm := F.left + SHORT(P.left DIV Unit);
  516.         IF t # F.trailer THEN Texts.OpenReader(R, F.text, t.org); Texts.Read(R, nextCh);
  517.             i := 0; n := 0; DEC(pos, t.org); dx := 0; mw := F.W - F.right;
  518.             WHILE ~R.eot & (i < t.len) & (i <= pos) & (pw + dx <= mw) DO
  519.                 (* i ... pos of nextCh; dx ... width of char before nextCh; pw ... line width up to pos (or up to right margin) *)
  520.                 INC(i); INC(pw, dx);
  521.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, x, y, w, h)
  522.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, x, y, w, h, pat)
  523.                 END;
  524.                 dy := R.fnt.height * R.voff DIV 64;
  525.                 Texts.Read(R, nextCh)
  526.             END;
  527.             IF (i <= pos) & (pw + dx <= mw) THEN INC(i); INC(pw, dx) END
  528.         ELSE dx := 4
  529.         END
  530.     END Width;
  531.     PROCEDURE LocatePos* (F: Frame; pos: LONGINT; VAR loc: Location);    (* loc.dx = dx of char at pos *)
  532.         VAR t: TextLine; pw, dx, dy: INTEGER;
  533.     BEGIN
  534.         IF pos < F.org THEN pos := F.org; t := F.trailer.next
  535.         ELSIF pos < F.trailer.org THEN t := F.trailer;
  536.             WHILE (t.next # F.trailer) & (t.next.org <= pos) DO t := t.next END
  537.         ELSE pos := F.trailer.org; t := F.trailer.next;
  538.             WHILE ~t.eot DO t := t.next END
  539.         END;
  540.         Width(F, t, pos, pw, dx, dy); LocateLineTop(F, F.trailer, t.org, loc); DEC(loc.y, loc.line.h);
  541.         loc.org := t.org; loc.pos := pos; loc.x := F.X + pw; loc.dx := dx; loc.dy := dy; loc.line := t
  542.     END LocatePos;
  543.     PROCEDURE LocateLine* (F: Frame; y: INTEGER; VAR loc: Location);
  544.         (* loc.x = line start; loc.y = line bottom; loc.dx = line width *)
  545.         VAR t: TextLine; h, ph, pw, tw, ddx, cn: INTEGER;
  546.     BEGIN
  547.         t := F.trailer.next; h := y - F.Y; ph := F.H - F.top - t.h;
  548.         WHILE ~t.eot & (ph - t.next.h >= F.bot) & (ph > h) DO t := t.next; DEC(ph, t.h) END;
  549.         AdjustMetrics(F, t, pw, tw, ddx, cn);
  550.         IF pw >= F.W - F.right THEN pw := F.W - F.right - 4 END;
  551.         loc.org := t.org; loc.pos := loc.org; loc.x := F.X + pw; loc.y := F.Y + ph; loc.dx := tw; loc.dy := 0; loc.line := t
  552.     END LocateLine;
  553.     PROCEDURE LocateChar* (F: Frame; x, y: INTEGER; VAR loc: Location);
  554.         VAR t: TextLine; pat: Display.Pattern; i: LONGINT; n, w, lm, pw, tw, ddx, cn, dx, xc, yc, wc, hc: INTEGER;
  555.     BEGIN
  556.         LocateLine(F, y, loc); t := loc.line; w := x - F.X; AdjustMetrics(F, t, pw, tw, ddx, cn);
  557.         lm := F.left + SHORT(P.left DIV Unit);
  558.         IF (t # F.trailer) & (w > pw) THEN Texts.OpenReader(R, F.text, t.org);
  559.             i := 0; n := 0; dx := 0; nextCh := 0X;
  560.             WHILE (i < t.len) & (pw + dx < w) DO
  561.                 (* i = pos after nextCh; dx = width of nextCh; pw = line width without nextCh *)
  562.                 Texts.Read(R, nextCh); INC(i); INC(pw, dx);
  563.                 IF nextCh <= " " THEN GetSpecial(F, n, cn, ddx, pw - lm, dx, xc, yc, wc, hc)
  564.                 ELSE Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  565.                 END
  566.             END;
  567.             IF pw + dx < w THEN INC(i); INC(pw, dx); R.elem := NIL END;
  568.             INC(loc.pos, i - 1); loc.x := F.X + pw;
  569.             IF i < t.len THEN loc.dx := dx; loc.dy := R.fnt.height * R.voff DIV 64 ELSE loc.dx := 4 END
  570.         ELSE loc.dx := 4; R.elem := NIL
  571.         END
  572.     END LocateChar;
  573.     PROCEDURE LocateWord* (F: Frame; x, y: INTEGER; VAR loc: Location);
  574.         VAR t: TextLine; pos, i: LONGINT; px, rx: INTEGER; pat: Display.Pattern; dx, xc, yc, wc, hc: INTEGER;
  575.     BEGIN
  576.         LocateChar(F, x, y, loc); pos := loc.pos + 1;
  577.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  578.         UNTIL (pos < loc.org) OR (nextCh > " ");
  579.         INC(pos);
  580.         REPEAT DEC(pos); Texts.OpenReader(R, F.text, pos); Texts.Read(R, nextCh)
  581.         UNTIL (pos < loc.org) OR (nextCh <= " ");
  582.         LocatePos(F, pos + 1, loc); t := loc.line; i := loc.pos - loc.org;
  583.         IF i < t.len THEN px := loc.x; rx := F.X + F.W - F.right;
  584.             Texts.OpenReader(R, F.text, loc.pos); dx := 0; wc := 0; nextCh := "x";
  585.             WHILE (i < t.len) & (nextCh > " ") & (px + dx < rx) DO
  586.                 Texts.Read(R, nextCh); INC(i); INC(px, dx);
  587.                 Display.GetChar(R.fnt.raster, nextCh, dx, xc, yc, wc, hc, pat)
  588.             END;
  589.             IF (nextCh > " ") & (px + dx < rx) THEN INC(i); INC(px, dx) END;
  590.             loc.dx := px - loc.x
  591.         ELSE loc.dx := 0
  592.         END
  593.     END LocateWord;
  594.     PROCEDURE Pos* (F: Frame; x, y: INTEGER): LONGINT;
  595.         VAR loc: Location;
  596.     BEGIN LocateChar(F, x, y, loc); RETURN loc.pos
  597.     END Pos;
  598.     PROCEDURE ThisSubFrame (F: Frame; x, y: INTEGER): Display.Frame;
  599.         VAR f: Display.Frame;
  600.     BEGIN f := F.dsc;
  601.         WHILE (f # NIL) & ((x < f.X) OR (x >= f.X + f.W) OR (y < f.Y) OR (y >= f.Y + f.H)) DO f := f.next END;
  602.         RETURN f
  603.     END ThisSubFrame;
  604.     (** Caret & Selection **)
  605.     PROCEDURE PassSubFocus (F: Frame; f: Display.Frame);
  606.         (* pass focus from F.focus to f (f is also an element frame in F) *)
  607.         VAR loc: Location; f1: Display.Frame; ctrl: Oberon.ControlMsg; focus: FocusMsg;
  608.     BEGIN
  609.         IF F.focus # NIL THEN f1 := F.focus;
  610.             ctrl.id := Oberon.defocus; f1.handle(f1, ctrl);
  611.             LocateChar(F, f1.X + 1, f1.Y + 1, loc);
  612.             InvertBorder(f1); F.focus := NIL;
  613.             IF R.elem # NIL THEN
  614.                 focus.focus := FALSE; focus.elemFrame := f1; focus.frame := F; R.elem.handle(R.elem, focus)
  615.             END
  616.         END;
  617.         IF f # NIL THEN
  618.             LocateChar(F, f.X + 1, f.Y + 1, loc);    (* side effect: set R to element *)
  619.             focus.focus := TRUE; focus.elemFrame := f; focus.frame := F; R.elem.handle(R.elem, focus);
  620.             InvertBorder(f)
  621.         END;
  622.         F.focus := f
  623.     END PassSubFocus;
  624.     PROCEDURE RemoveSelection* (F: Frame);
  625.     BEGIN
  626.         IF F.hasSel THEN InvertSelection(F, F.selbeg, F.selend); F.hasSel := FALSE END
  627.     END RemoveSelection;
  628.     PROCEDURE SetSelection* (F: Frame; beg, end: LONGINT);    (** forces range to visible bounds **)
  629.         VAR loc: Location;
  630.     BEGIN
  631.         IF end > F.text.len THEN end := F.text.len END;
  632.         IF end > beg THEN
  633.             IF F.hasSel & (F.selbeg.pos = beg) THEN
  634.                 IF (F.selend.pos < end) & (F.selend.pos < F.trailer.org) THEN
  635.                     LocatePos(F, F.selend.pos, loc); LocatePos(F, end, F.selend); InvertSelection(F, loc, F.selend)
  636.                 ELSIF end < F.selend.pos THEN
  637.                     LocatePos(F, end, loc); InvertSelection(F, loc, F.selend); LocatePos(F, end, F.selend)
  638.                 END
  639.             ELSE RemoveSelection(F); PassSubFocus(F, NIL);
  640.                 LocatePos(F, beg, F.selbeg); LocatePos(F, end, F.selend); InvertSelection(F, F.selbeg, F.selend)
  641.             END;
  642.             F.hasSel := TRUE; F.time := Oberon.Time()
  643.         END
  644.     END SetSelection;
  645.     PROCEDURE RemoveCaret* (F: Frame);
  646.         VAR msg: Oberon.ControlMsg;
  647.     BEGIN
  648.         IF F.focus # NIL THEN msg.id := Oberon.defocus; F.focus.handle(F.focus, msg) END;
  649.         IF F.hasCar THEN InvertCaret(F); F.hasCar := FALSE END
  650.     END RemoveCaret;
  651.     PROCEDURE SetCaret* (F: Frame; pos: LONGINT);    (** only done if within visible bounds **)
  652.     BEGIN
  653.         IF ~F.hasCar OR (F.carloc.pos # pos) THEN RemoveCaret(F); PassSubFocus(F, NIL);
  654.             LocatePos(F, pos, F.carloc);
  655.             IF F.carloc.x <= F.X + F.W - F.right THEN InvertCaret(F); F.hasCar := TRUE END
  656.         END
  657.     END SetCaret;
  658.     (** Display Range **)
  659.     PROCEDURE Complete (F: Frame; trailer: TextLine; s: TextLine; org: LONGINT; ph: INTEGER);
  660.         VAR u: TextLine;
  661.     BEGIN
  662.         IF ph > F.bot THEN    (*try to add new lines to the bottom*)
  663.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);
  664.             LOOP
  665.                 IF R.eot THEN EXIT END;
  666.                 NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  667.                 IF ph - u.h < F.bot THEN EXIT END;
  668.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span)
  669.             END
  670.         END;
  671.         s.next := trailer; trailer.eot := TRUE; trailer.org := org; trailer.len := 0; trailer.w := 0;
  672.         trailer.h := SHORT(defParc.lsp DIV Unit); trailer.P := P; trailer.pbeg := pbeg
  673.     END Complete;
  674.     PROCEDURE ShowFrom (F: Frame; pos: LONGINT);    (* removes global marks as needed and neutralizes F *)
  675.         VAR new, s: TextLine; beg, end: Location; org: LONGINT; ph, y0, dy: INTEGER;
  676.     BEGIN
  677.         F.handle(F, neutralize);
  678.         IF (F.trailer # NIL) & (F.org < pos) & (pos < F.trailer.org) THEN    (* shift up and extend to the bottom *)
  679.             LocateLineTop(F, F.trailer, pos, beg); LocateLineTop(F, F.trailer, F.trailer.org, end);
  680.             dy := (F.Y + F.H - F.top) - beg.y; Shift(F, end.y, end.y + dy, beg.y - end.y);
  681.             Erase(F, F.X + F.left, end.y, F.W - F.left, dy);
  682.             s := F.trailer.next; WHILE s.org # pos DO s := s.next END;
  683.             F.trailer.next := s; org := s.org + s.span; ph := F.H - F.top - s.h;
  684.             WHILE s.next # F.trailer DO s := s.next; org := org + s.span; ph := ph - s.h END;
  685.             Complete(F, F.trailer, s, org, ph); F.org := pos; ShowLines(F, F.bot, end.y + dy - F.Y, FALSE)
  686.         ELSIF (F.trailer = NIL) OR (pos # F.org) THEN
  687.             MeasureLines(F, pos, new);
  688.             IF (F.trailer # NIL) & (pos < F.org) & (F.org <= new.org) THEN    (* shift down and extend to the top *)
  689.                 LocateLineTop(F, new, F.org, beg); LocateLineTop(F, new, new.org, end);
  690.                 y0 := F.Y + F.H - F.top; Shift(F, y0 - (beg.y - end.y), end.y, beg.y - end.y);
  691.                 Erase(F, F.X + F.left, beg.y, F.W - F.left, y0 - beg.y);
  692.                 Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, end.y - (F.Y + F.bot));
  693.                 F.org := pos; F.trailer := new; ShowLines(F, beg.y - F.Y, F.H - F.top, FALSE)
  694.             ELSE    (* full redisplay *)
  695.                 IF F.trailer = NIL THEN Erase(F, F.X, F.Y, F.W, F.H); ShowBar(F, 0, F.H); F.markH := -1
  696.                 ELSE Erase(F, F.X + F.left, F.Y + F.bot, F.W - F.left, F.H - F.bot - F.top)
  697.                 END;
  698.                 F.org := pos; F.trailer := new; ShowLines(F, F.bot, F.H - F.top, FALSE)
  699.             END
  700.         END;
  701.         ShowTick(F)
  702.     END ShowFrom;
  703.     PROCEDURE Show* (F: Frame; pos: LONGINT);    (** removes global marks as needed and neutralizes F **)
  704.     BEGIN BegOfLine(F.text, pos, TRUE); ShowFrom(F, pos)
  705.     END Show;
  706.     PROCEDURE Resize (F: Frame; x, y, w, h: INTEGER);
  707.         VAR oldY, oldH, dh, ph: INTEGER; t: TextLine;
  708.     BEGIN
  709.         IF (w = 0) OR (h = 0) THEN InvalSubFrames(F, F.X, F.Y, F.W, F.H);
  710.             F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL
  711.         ELSIF (F.trailer # NIL) & (x = F.X) & (w = F.W) THEN
  712.             oldY := F.Y; oldH := F.H; Tick(F); F.markH := -1; F.Y := y; F.H := h;
  713.             IF h > oldH THEN dh := h - oldH;    (* extend *)
  714.                 IF y + h # oldY + oldH THEN
  715.                     Display.CopyBlock(x, oldY, w, oldH, x, y + dh, Display.replace);
  716.                     ShiftSubFrames(F, oldY, y + dh, oldH)
  717.                 END;
  718.                 EraseRect(F, x, y, w, dh); ShowBar(F, 0, dh);
  719.                 t := F.trailer; ph := F.H - F.top;
  720.                 WHILE t.next # F.trailer DO t := t.next; ph := ph - t.h END;
  721.                 Complete(F, F.trailer, t, F.trailer.org, ph); ShowLines(F, F.bot, ph, FALSE)
  722.             ELSE dh := oldH - h;    (* reduce *)
  723.                 IF y + h # oldY + oldH THEN
  724.                     Display.CopyBlock(x, oldY + dh, w, h, x, y, Display.replace);
  725.                     ShiftSubFrames(F, oldY + dh, y, h)
  726.                 END;
  727.                 t := F.trailer; ph := F.H - F.top;
  728.                 WHILE (t.next # F.trailer) & (ph - t.next.h >= F.bot) DO t := t.next; DEC(ph, t.h) END;
  729.                 IF t = F.trailer THEN t.org := F.org; t.span := 0 END;
  730.                 Complete(F, F.trailer, t, t.org + t.span, ph);
  731.                 EraseRect(F, x + F.left, y, w - F.left, ph);
  732.                 InvalSubFrames(F, x, oldY, w, y - oldY); InvalSubFrames(F, x, y + h, w, dh - (y - oldY))
  733.             END;
  734.             ShowTick(F)
  735.         ELSE F.X := x; F.Y := y; F.W := w; F.H := h; F.trailer := NIL; Show(F, F.org)
  736.         END
  737.     END Resize;
  738.     (** Contents Update **)
  739.     PROCEDURE Update (F: Frame; VAR msg: UpdateMsg);    (** removes global marks as needed **)
  740.         VAR t: TextLine; org, d, Fbeg, Fend: LONGINT;
  741.             foc: Display.Frame; beg, end: LONGINT; ch: CHAR; r: Texts.Reader; loc: Location;
  742.         PROCEDURE Begin (VAR beg: LONGINT; VAR org0: LONGINT; VAR q: TextLine);
  743.             (* org0 = origin of first affected line; beg = pos of first modified character; q = first affected line (if line origin has not moved).*)
  744.             (* q = NIL => beg = org0; q # NIL => first (beg-org0) characters of q need not be redrawn *)
  745.             VAR trailer, t: TextLine;
  746.         BEGIN
  747.             trailer := F.trailer; t := trailer;
  748.             WHILE (t.next # trailer) & (beg >= t.next.org + t.next.span) & ~t.next.eot DO t := t.next END;
  749.             q := t.next;
  750.             IF (t # trailer) & (q # trailer) & (beg <= q.org + q.span) THEN
  751.                 Texts.OpenReader(R, F.text, t.org); Texts.Read(R, nextCh); org0 := t.org; NextLine(F.text, org0)
  752.             ELSE org0 := beg; BegOfLine(F.text, org0, TRUE)
  753.             END;
  754.             IF org0 # q.org THEN
  755.                 IF t = trailer THEN org0 := q.org ELSE org0 := t.org END;
  756.                 beg := org0; q := NIL
  757.             END
  758.         END Begin;
  759.         PROCEDURE Adjust (end, delta: LONGINT);
  760.             (* H1 = top of synchronization line in old frame *)
  761.             (* h0 = top of line that was modified *)
  762.             (* h1 = top of block in new frame that could be reused *)
  763.             (* h2 = bottom of last line in new frame *)
  764.             (* h1 - h2 = height of block that could be reused *)
  765.             VAR new, old, s, t, u, p, q: TextLine; bot: Location;
  766.                 org, org0, beg: LONGINT; ph, h0, h1, H1, h2, lm, dx, dy: INTEGER;
  767.         BEGIN
  768.             q := NIL; LocateLineTop(F, F.trailer, F.trailer.org, bot);
  769.             IF msg.beg < F.org THEN org0 := F.org; beg := org0 ELSE beg := msg.beg; Begin(beg, org0, q) END;
  770.             NEW(new); s := new; old := F.trailer; t := old; org := F.org; ph := F.H - F.top;
  771.             WHILE (t.next # old) & (t.next.org # org0) DO t := t.next;    (*transfer unchanged prefix*)
  772.                 s.next := t; s := t; DEC(ph, s.h); INC(org, s.span)
  773.             END;
  774.             h0 := ph; H1 := h0; t := t.next; p := s;
  775.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh);    (*rebuild at least one line descriptor*)
  776.             LOOP NEW(u); u.org := org; MeasureLine(F, F.W - F.left - F.right, u);
  777.                 IF ph - u.h < F.bot THEN h1 := ph; h2 := h1; EXIT END;
  778.                 s.next := u; s := s.next; DEC(ph, s.h); INC(org, s.span);
  779.                 IF R.eot THEN h1 := ph; h2 := h1; EXIT END;
  780.                 IF org > end THEN
  781.                     WHILE (t # old) & (org > t.org + delta) DO DEC(H1, t.h); t := t.next END;
  782.                     IF (org = t.org + delta) & (P = t.P) THEN h1 := ph;    (*resynchronized*)
  783.                         WHILE (t # old) & (ph - t.h >= F.bot) DO    (*transfer unchanged suffix*)
  784.                             s.next := t; s := t; s.org := org; ParcBefore(F.text, s.org, s.P, s.pbeg);
  785.                             DEC(ph, s.h); INC(org, s.span); t := t.next
  786.                         END;
  787.                         h2 := ph; EXIT
  788.                     END
  789.                 END
  790.             END;
  791.             Shift(F, F.Y + H1 - (h1 - h2), F.Y + h2, h1 - h2);
  792.             Complete(F, new, s, org, ph); F.trailer := new; t := p.next;
  793.             IF (q # NIL) & (t # F.trailer) & (q.h = t.h) & (q.dsr = t.dsr) & (q.org = t.org) & (q.P = t.P) & (end <= t.org + t.span) THEN
  794.                 P := t.P; pbeg := t.pbeg;
  795.                 IF (P.opts * AdjMask = {leftAdj}) OR (P.opts * AdjMask = AdjMask) & (q.nob = 0) & (t.nob = 0) THEN
  796.                     Width(F, t, beg, lm, dx, dy);    (*preserve prefix of first affected line*)
  797.                     DEC(h0, t.h); Erase(F, F.X + lm, F.Y + h0, F.W - lm, t.h);
  798.                     ShowLine(F, t, F.X + lm, F.X + F.W - F.right, F.Y + h0)
  799.                 END
  800.             END;
  801.             ShowLines(F, h1, h0, TRUE);
  802.             Erase(F, F.X + F.left, bot.y, F.W - F.left, h2 - (bot.y - F.Y)); ShowLines(F, F.bot, h2, FALSE)
  803.         END Adjust;
  804.     BEGIN
  805.         foc := F.focus; beg := msg.beg; end := msg.end;
  806.         F.handle(F, neutralize); MarkMenu(F); Fbeg := F.org; Fend := F.trailer.org;
  807.         IF (msg.id = Texts.insert) & (msg.beg < F.org) THEN t := F.trailer; d := msg.end - msg.beg; INC(F.org, d);
  808.             REPEAT INC(t.org, d); t := t.next UNTIL t = F.trailer
  809.         ELSIF msg.id = Texts.delete THEN
  810.             IF msg.end <= F.org THEN t := F.trailer; d := msg.end - msg.beg; DEC(F.org, d);
  811.                 REPEAT DEC(t.org, d); t := t.next UNTIL t = F.trailer
  812.             ELSIF msg.beg < F.org THEN F.org := msg.beg
  813.             END
  814.         END;
  815.         org := F.org;
  816.         IF msg.beg <= Fbeg + AdjustSpan THEN BegOfLine(F.text, org, TRUE) END;
  817.         ParcBefore(F.text, org, P, d);
  818.         IF (org # F.org) OR (P # F.trailer.next.P) THEN
  819.             F.trailer := NIL; Show(F, F.org)
  820.         ELSIF (msg.end > Fbeg) & (msg.beg < Fend + AdjustSpan) THEN
  821.             IF msg.id = Texts.replace THEN Adjust(msg.end, 0);
  822.                 (* refocus element if necessary *)
  823.                 IF (foc # NIL) & (end-beg = 1) THEN
  824.                     Texts.OpenReader(r, F.text, beg); Texts.Read(r, ch);
  825.                     IF r.elem # NIL THEN
  826.                         LocatePos(F, beg, loc); foc := ThisSubFrame(F, loc.x, loc.y); PassSubFocus(F, foc)
  827.                     END
  828.                 END
  829.             ELSIF msg.id = Texts.insert THEN Adjust(msg.end, msg.end - msg.beg)
  830.             ELSIF msg.id = Texts.delete THEN Adjust(msg.beg, msg.beg - msg.end)
  831.             END
  832.         END;
  833.         ShowTick(F)
  834.     END Update;
  835.     (** User Interface **)
  836.     PROCEDURE Back (F: Frame; dY: INTEGER; (*inout*) VAR org: LONGINT);    (* mh 10.10.92 *)
  837.         (* computes new org such that old org is (at most) dY pixels below new org *)
  838.         VAR H: INTEGER; oldOrg: LONGINT;
  839.         PROCEDURE TotalHeight (org1, org2: LONGINT): INTEGER;
  840.             (* measures total height of text-lines starting at org1 and ending at the line before the line containing org2 *)
  841.             VAR h: INTEGER; line: TextLine;
  842.         BEGIN
  843.             Texts.OpenReader(R, F.text, org1); Texts.Read(R, nextCh); NEW(line); h := 0;
  844.             LOOP line.org := org1;
  845.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org1, line.span);
  846.                 IF Texts.Pos(R)-1 > org2 THEN EXIT END;
  847.                 INC(h, line.h);
  848.                 IF R.eot THEN EXIT END
  849.             END;
  850.             RETURN h
  851.         END TotalHeight;
  852.         PROCEDURE Forward (h: INTEGER);
  853.             (* increase org by n text-lines such that the sum of the n line-heights > h *)
  854.             VAR line: TextLine;
  855.         BEGIN
  856.             Texts.OpenReader(R, F.text, org); Texts.Read(R, nextCh); NEW(line);
  857.             WHILE h > 0 DO line.org := org;
  858.                 MeasureLine(F, F.W - F.left - F.right, line); INC(org, line.span); DEC(h, line.h)
  859.             END;
  860.             org := Texts.Pos(R)-1
  861.         END Forward;
  862.     BEGIN H := 0;
  863.         LOOP oldOrg := org;
  864.             IF org = 0 THEN EXIT END;
  865.             DEC(org, 800); BegOfLine(F.text, org, FALSE);
  866.             INC(H, TotalHeight(org, oldOrg));
  867.             IF H > dY THEN EXIT END
  868.         END;
  869.         Forward(H - dY)
  870.     END Back;
  871.     PROCEDURE TrackLine* (F: Frame; VAR x, y: INTEGER; VAR org: LONGINT; VAR keysum: SET);
  872.         VAR keys: SET; new, old: Location;
  873.     BEGIN
  874.         LocateLine(F, y, old); InvertRect(F, old.x, old.y, old.dx + 4, 2); keysum := {};
  875.         REPEAT TrackMouse(x, y, keys, keysum); LocateLine(F, y, new);
  876.             IF new.org # old.org THEN
  877.                 InvertRect(F, new.x, new.y, new.dx + 4, 2); InvertRect(F, old.x, old.y, old.dx + 4, 2); old := new
  878.             END
  879.         UNTIL keys = {};
  880.         InvertRect(F, new.x, new.y, new.dx + 4, 2); org := new.org
  881.     END TrackLine;
  882.     PROCEDURE TrackWord* (F: Frame; VAR x, y: INTEGER; VAR pos: LONGINT; VAR keysum: SET);
  883.         VAR keys: SET; new, old: Location;
  884.     BEGIN
  885.         LocateWord(F, x, y, old); InvertRect(F, old.x, old.y, old.dx, 2); keysum := {};
  886.         REPEAT TrackMouse(x, y, keys, keysum); LocateWord(F, x, y, new);
  887.             IF new.pos # old.pos THEN
  888.                 InvertRect(F, new.x, new.y, new.dx, 2); InvertRect(F, old.x, old.y, old.dx, 2); old := new
  889.             END
  890.         UNTIL keys = {};
  891.         InvertRect(F, new.x, new.y, new.dx, 2); pos := new.pos
  892.     END TrackWord;
  893.     PROCEDURE TrackCaret* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  894.         VAR keys: SET;
  895.     BEGIN keysum := {};
  896.         REPEAT TrackMouse(x, y, keys, keysum); SetCaret(F, Pos(F, x, y)) UNTIL keys = {}
  897.     END TrackCaret;
  898.     PROCEDURE TrackSelection* (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  899.         VAR keys: SET; pos: LONGINT; V: Viewers.Viewer; f: Frame;
  900.     BEGIN
  901.         V := Viewers.This(F.X, F.Y); V := V.next(Viewers.Viewer);
  902.         IF (V.dsc # NIL) & (V.dsc.next # NIL) & (V.dsc.next IS Frame) THEN f := V.dsc.next(Frame);
  903.             IF f.hasSel & (f.text = F.text) THEN
  904.                 IF (f.selbeg.pos < f.trailer.org) & (f.org < f.selend.pos) & (f.selbeg.pos <= Pos(F, x, y)) THEN
  905.                     SetSelection(F, f.selbeg.pos, Pos(F, x, y) + 1)
  906.                 ELSE RemoveSelection(f); f := NIL
  907.                 END
  908.             ELSE f := NIL
  909.             END
  910.         ELSE f := NIL
  911.         END;
  912.         IF f = NIL THEN
  913.             IF F.hasSel & (F.selbeg.pos + 1 = F.selend.pos) & (Pos(F, x, y) = F.selbeg.pos) THEN
  914.                 SetSelection(F, F.selbeg.org, Pos(F, x, y) + 1)
  915.             ELSE SetSelection(F, Pos(F, x, y), Pos(F, x, y) + 1)
  916.             END
  917.         END;
  918.         keysum := {};
  919.         REPEAT TrackMouse(x, y, keys, keysum);
  920.             IF F.hasSel THEN
  921.                 pos := Pos(F, x, Min(y, F.selbeg.y)) + 1;
  922.                 IF pos <= F.selbeg.pos THEN pos := F.selbeg.pos + 1 END;
  923.                 SetSelection(F, F.selbeg.pos, pos);
  924.                 IF f # NIL THEN SetSelection(f, f.selbeg.pos, pos); f.selend.pos := F.selend.pos END
  925.             ELSE SetSelection(F, Pos(F, x, y), Pos(F, x, y) + 1)
  926.             END
  927.         UNTIL keys = {};
  928.         IF f # NIL THEN F.selbeg.pos := f.selbeg.pos END
  929.     END TrackSelection;
  930.     PROCEDURE^ NewText* (T: Texts.Text; pos: LONGINT): Frame;
  931.     PROCEDURE^ NewMenu* (name, commands: ARRAY OF CHAR): Frame;
  932.     PROCEDURE^ Text* (name: ARRAY OF CHAR): Texts.Text;
  933.     PROCEDURE Call (F: Frame; pos: LONGINT; keysum: SET);
  934.         VAR S: Texts.Scanner; res, i, j, X, Y: INTEGER; v: Viewers.Viewer;
  935.     BEGIN
  936.         Texts.OpenScanner(S, F.text, pos); Texts.Scan(S);
  937.         IF (S.class = Texts.Name) & (S.line = 0) THEN
  938.             IF rightKey IN keysum THEN  (*open text viewer*)
  939.                 S.s := "Edit.Open"; par.pos := pos
  940.             ELSE par.pos := pos + S.len
  941.             END ;
  942.             par.vwr := Viewers.This(F.X, F.Y);
  943.             par.frame := F; par.text := F.text;
  944.             Oberon.Call(S.s, par, keysum = {middleKey, leftKey}, res);
  945.             IF res > 0 THEN
  946.                 Texts.WriteString(W0, "Call error: "); Texts.WriteString(W0, Modules.importing);
  947.                 IF res = 1 THEN Texts.WriteString(W0, " not found")
  948.                 ELSIF res = 2 THEN Texts.WriteString(W0, " not an obj-file")
  949.                 ELSIF res = 3 THEN Texts.WriteString(W0, " imports ");
  950.                     Texts.WriteString(W0, Modules.imported); Texts.WriteString(W0, " with bad key")
  951.                 ELSIF res = 4 THEN Texts.WriteString(W0, " corrupted obj file")
  952.                 ELSIF res = 6 THEN Texts.WriteString(W0, " has too many imports")
  953.                 ELSIF res = 7 THEN Texts.WriteString(W0, " not enough space")
  954.                 END
  955.             ELSIF res < 0 THEN
  956.                 INC(i); WHILE i < S.len DO Texts.Write(W0, S.s[i]); INC(i) END;
  957.                 Texts.WriteString(W0, " not found")
  958.             END;
  959.             IF res # 0 THEN Texts.WriteLn(W0); Texts.Append(Oberon.Log, W0.buf) END
  960.         END
  961.     END Call;
  962.     PROCEDURE PickAttributes (VAR W: Texts.Writer; T: Texts.Text; pos: LONGINT; font: Fonts.Font; col, voff: SHORTINT);
  963.         VAR R: Texts.Reader; ch: CHAR;
  964.     BEGIN
  965.         IF T.len > 0 THEN
  966.             IF pos < T.len THEN Texts.OpenReader(R, T, pos); Texts.Read(R, ch) END;
  967.             IF (pos > 0) & ((pos = T.len) OR (ch <= " ")) THEN
  968.                 Texts.OpenReader(R, T, pos - 1); Texts.Read(R, ch)
  969.             END;
  970.             Texts.SetFont(W, R.fnt); Texts.SetColor(W, R.col);
  971.             IF (ch = CR) OR (ch = TAB) OR (ch = LF) THEN Texts.SetOffset(W, voff) ELSE Texts.SetOffset(W, R.voff) END
  972.         ELSE Texts.SetFont(W, font); Texts.SetColor(W, col); Texts.SetOffset(W, voff)
  973.         END
  974.     END PickAttributes;
  975.     PROCEDURE ShiftBlock (F: Frame; delta: INTEGER);    (* shift selected lines to left or right *)
  976.         VAR text: Texts.Text; pos, beg, end, time: LONGINT; select: SelectMsg; ch: CHAR;
  977.     BEGIN
  978.         Oberon.GetSelection(text, beg, end, time);
  979.         IF (time >= 0) & (text = F.text) THEN BegOfLine(F.text, beg, FALSE); pos := beg;
  980.             WHILE pos < end DO Texts.OpenReader(R, F.text, pos); Texts.Read(R, ch);
  981.                 WHILE (R.elem # NIL) & (R.elem IS Parc) & (pos < end) DO Texts.Read(R, ch); INC(pos) END;
  982.                 IF pos < end THEN
  983.                     IF delta < 0 THEN
  984.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN
  985.                             Texts.Delete(F.text, pos, pos + 1); DEC(end)
  986.                         END
  987.                     ELSE
  988.                         PickAttributes(W, text, pos, Oberon.CurFnt, Oberon.CurCol, Oberon.CurOff);
  989.                         IF (ch <= " ") & (ch # CR) & (ch # Texts.ElemChar) THEN Texts.Write(W, ch)    (* first char extension *)
  990.                         ELSE Texts.Write(W, TAB)
  991.                         END;
  992.                         Texts.Insert(F.text, pos, W.buf); INC(end); INC(pos)
  993.                     END;
  994.                     Texts.OpenReader(R, F.text, pos);
  995.                     REPEAT Texts.Read(R, ch) UNTIL R.eot OR (ch = CR);
  996.                     pos := Texts.Pos(R)
  997.                 END
  998.             END;
  999.             select.text := F.text; select.beg := beg; select.end := pos; select.time := Oberon.Time();
  1000.             Viewers.Broadcast(select)
  1001.         END
  1002.     END ShiftBlock;
  1003.     PROCEDURE Write (F: Frame; ch: CHAR; fnt: Fonts.Font; col, voff: SHORTINT);
  1004.         VAR loc: Location; parc: Parc; org, pos, pbeg: LONGINT; i: INTEGER; ch0: CHAR;
  1005.             buf: ARRAY 32 OF CHAR;
  1006.             copy: Texts.CopyMsg; input: Oberon.InputMsg;
  1007.         PROCEDURE Visible(ch: CHAR): BOOLEAN;
  1008.             VAR pat: Display.Pattern; dx, x, y, w, h: INTEGER;
  1009.         BEGIN Display.GetChar(W.fnt.raster, ch, dx, x, y, w, h, pat); RETURN dx > 0
  1010.         END Visible;
  1011.         PROCEDURE InsertBuffer;
  1012.             VAR i, j: INTEGER; ch: CHAR;
  1013.         BEGIN i := 0; j := 0; ch := buf[i];
  1014.             WHILE ch # 0X DO
  1015.                 IF (ch = TAB) OR (ch = CR) OR (ch = " ") OR Visible(ch) THEN Texts.Write(W, ch); INC(j) END;
  1016.                 INC(i); ch := buf[i]
  1017.             END;
  1018.             IF j > 0 THEN Texts.Insert(F.text, pos, W.buf); INC(pos, LONG(j)) END
  1019.         END InsertBuffer;
  1020.         PROCEDURE Flush;
  1021.             VAR ch: CHAR;
  1022.         BEGIN
  1023.             WHILE Input.Available() > 0 DO Input.Read(ch) END
  1024.         END Flush;
  1025.     BEGIN
  1026.         IF F.hasSel & (ch = CRSL) THEN ShiftBlock(F, -1)
  1027.         ELSIF F.hasSel & (ch = CRSR) THEN ShiftBlock(F, 1)
  1028.         ELSIF F.hasCar THEN pos := F.carloc.pos;
  1029.             IF (ch = DEL) & (pos > F.org) THEN DEC(pos); Texts.Delete(F.text, pos, pos + 1); Flush
  1030.             ELSIF (ch = CRSL) & (pos > 0) THEN DEC(pos)
  1031.             ELSIF (ch = CRSR) & (pos < F.text.len) THEN INC(pos)
  1032.             ELSIF (ch = CRSU) & (pos > 0) THEN                                    (*<< mah cursor up *)
  1033.                 org:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h);
  1034.                 IF org=pos THEN Show (F, F.org-1) END;
  1035.                 pos:=Pos (F, F.carloc.x+1, F.carloc.y+F.carloc.line.h)
  1036.             ELSIF (ch = CRSD) & (pos < F.text.len) THEN                        (*<< mah cursor down *)
  1037.                 org:=Pos (F, F.carloc.x+1, F.carloc.y-F.carloc.line.next.h);
  1038.                 IF (org=pos) & (F.trailer.org+F.trailer.len#F.text.len) THEN Show (F, F.trailer.next.next.org) END;
  1039.                 LocatePos (F, pos, loc);
  1040.                 pos:=Pos (F, F.carloc.x+1, loc.y-loc.line.next.h)
  1041.             ELSIF (ch = BRK) OR (ch = ShiftBRK) THEN
  1042.                 ParcBefore(F.text, pos, P, pbeg); P.handle(P, copy); parc := copy.e(Parc);
  1043.                 IF ch = BRK THEN EXCL(parc.opts, pageBreak) ELSE INCL(parc.opts, pageBreak) END;
  1044.                 PickAttributes(W, F.text, pos, fnt, col, voff);
  1045.                 Texts.WriteElem(W, parc); Texts.Insert(F.text, pos, W.buf); INC(pos)
  1046.             ELSIF (ch = TAB) OR (ch = LF) OR (ch = CR) OR (ch >= " ") THEN
  1047.                 PickAttributes(W, F.text, pos, fnt, col, voff);
  1048.                 IF ch = LF THEN buf[0] := CR; i := 1; org := F.carloc.org; BegOfLine(F.text, org, FALSE);
  1049.                     Texts.OpenReader(R, F.text, org);
  1050.                     REPEAT Texts.Read(R, ch) UNTIL (R.elem = NIL) OR ~(R.elem IS Parc);
  1051.                     WHILE (Texts.Pos(R) <= pos) & (ch <= " ") & (ch # Texts.ElemChar) & (i < 31) DO
  1052.                         buf[i] := ch; INC(i); Texts.Read(R, ch)
  1053.                     END
  1054.                 ELSE buf[0] := ch; i := 1
  1055.                 END;
  1056.                 WHILE (Input.Available() > 0) & (i < 31) & (ch >= " ") & (ch < DEL) DO Input.Read(buf[i]); INC(i) END;
  1057.                 buf[i] := 0X; InsertBuffer
  1058.             END;
  1059.             IF pos < F.org THEN Show(F, F.org - 1)
  1060.             ELSIF pos < F.text.len THEN org := -1;
  1061.                 WHILE (pos >= F.trailer.org) & (pos > F.org) DO
  1062.                     org := F.trailer.next.next.org; IF org = F.org THEN INC(org) END;
  1063.                     ShowFrom(F, org); Flush
  1064.                 END
  1065.             ELSE LocatePos(F, pos, loc); LocateChar(F, loc.x + 1, loc.y, loc);
  1066.                 IF pos # loc.pos THEN Show(F, F.trailer.next.next.org); Flush END
  1067.             END;
  1068.             SetCaret(F, pos)
  1069.         ELSIF F.focus # NIL THEN input.id := Oberon.consume; input.ch := ch;
  1070.             input.fnt := fnt; input.col := col; input.voff := voff; F.focus.handle(F.focus, input)
  1071.         END
  1072.     END Write;
  1073.     PROCEDURE TouchElem (F: Frame; VAR x, y: INTEGER; VAR keysum: SET);
  1074.         VAR loc: Location; e: Texts.Elem; pbeg: LONGINT; y0: INTEGER;
  1075.             track: TrackMsg;
  1076.     BEGIN
  1077.         LocateChar(F, x, y, loc); e := R.elem;
  1078.         IF (e # NIL) & (loc.x + e.W DIV Unit <= F.X + F.W - F.right) THEN
  1079.             ParcBefore(F.text, loc.pos, P, pbeg); y0 := loc.y + loc.line.dsr - SHORT(P.dsr DIV Unit) + loc.dy;
  1080.             IF (loc.x <= x) & (x < loc.x + e.W DIV Unit) & (keysum= {middleKey}) THEN
  1081.                 track.X := x; track.Y := y; track.keys := keysum;
  1082.                 track.fnt := R.fnt; track.col := R.col; track.pos := Texts.Pos(R) - 1;
  1083.                 track.frame := F; track.X0 := loc.x; track.Y0 := y0;
  1084.                 e.handle(e, track); keysum := {}
  1085.             END
  1086.         END
  1087.     END TouchElem;
  1088.     PROCEDURE Edit (F: Frame; x, y: INTEGER; keysum: SET);
  1089.         VAR ef: Display.Frame; text: Texts.Text; beg, end, time, pos, oldpos: LONGINT; keys: SET; ch: CHAR;
  1090.             loc: Location; delta: INTEGER; copyover: Oberon.CopyOverMsg; input: Oberon.InputMsg;
  1091.     BEGIN
  1092.         IF x < F.X + F.barW THEN pos := F.org;    (* scroll bar *)
  1093.             IF leftKey IN keysum THEN TrackLine(F, x, y, pos, keysum)
  1094.             ELSIF rightKey IN keysum THEN TrackLine(F, x, y, pos, keysum); LocateLine(F, y, loc);
  1095.                 pos := F.org; delta := loc.y - (F.Y + F.bot); Back(F, delta, pos)
  1096.             ELSIF middleKey IN keysum THEN
  1097.                 (* heavy dragging *)
  1098.                 IF (F.Y + F.markH - 1 <= y) & (y < F.Y + F.markH + 4) THEN
  1099.                     oldpos := F.org;
  1100.                     REPEAT
  1101.                         TrackMouse(x, y, keys, keysum);
  1102.                         IF (F.Y <= y) & (y <= F.Y + F.H) THEN Show(F, CoordToPos(F, y - F.Y)); pos := F.org END
  1103.                     UNTIL keys # {middleKey};
  1104.                     REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1105.                     IF keysum = cancel THEN ShowFrom(F, oldpos) END
  1106.                 END;
  1107.                 REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1108.                 IF keysum = {middleKey, leftKey} THEN pos := F.text.len; (*BegOfLine(F.text, pos, TRUE);*)
  1109.                     Back(F, F.H - F.bot - F.top - 30 (*heuristic*), pos)
  1110.                 ELSIF keysum = {middleKey, rightKey} THEN pos := 0
  1111.                 ELSIF (F.Y <= y) & (y <= F.Y + F.H) THEN pos := CoordToPos(F, y - F.Y); BegOfLine(F.text, pos, TRUE)
  1112.                 END
  1113.             ELSE DrawCursor(x, y); keysum := cancel
  1114.             END;
  1115.             IF keysum # cancel THEN ShowFrom(F, pos) END
  1116.         ELSE    (* text area *)
  1117.             ef := ThisSubFrame(F, x, y);
  1118.             IF ef # NIL THEN    (* within sub-frame *)
  1119.                 IF (F.focus # ef) & (keysum = {leftKey}) THEN
  1120.                     REPEAT TrackMouse(x, y, keys, keysum) UNTIL keys = {};
  1121.                     IF keysum = {leftKey} THEN RemoveSelection(F); RemoveCaret(F); PassSubFocus(F, ef); RETURN END
  1122.                 ELSIF F.focus = ef THEN input.id := Oberon.track; input.keys := keysum; input.X := x; input.Y := y;
  1123.                     ef.handle(ef, input); RETURN
  1124.                 END
  1125.             END;
  1126.             IF keysum # {} THEN
  1127.                 TouchElem(F, x, y, keysum);
  1128.                 IF keysum = {} THEN RETURN END
  1129.             END;
  1130.             IF leftKey IN keysum THEN Oberon.PassFocus(Viewers.This(F.X, F.Y)); TrackCaret(F, x, y, keysum);
  1131.                 IF (keysum = {leftKey, middleKey}) & F.hasCar THEN Oberon.GetSelection(text, beg, end, time);
  1132.                     IF time >= 0 THEN Texts.Save(text, beg, end, B);
  1133.                         Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (end - beg))
  1134.                     END
  1135.                 ELSIF (keysum = {leftKey, rightKey}) & F.hasCar & (F.carloc.pos < F.text.len) THEN
  1136.                     Oberon.GetSelection(text, beg, end, time);
  1137.                     IF time >= 0 THEN Texts.OpenReader(R, F.text, F.carloc.pos); Texts.Read(R, ch);
  1138.                         Texts.ChangeLooks(text, beg, end, {0, 1, 2}, R.fnt, R.col, R.voff)
  1139.                     END
  1140.                 END
  1141.             ELSIF middleKey IN keysum THEN TrackWord(F, x, y, pos, keysum);
  1142.                 IF keysum # cancel THEN Call(F, pos, keysum) END
  1143.             ELSIF rightKey IN keysum THEN TrackSelection(F, x, y, keysum);
  1144.                 IF (keysum = {rightKey, middleKey}) & F.hasSel THEN
  1145.                     copyover.text := F.text; copyover.beg := F.selbeg.pos; copyover.end := F.selend.pos;
  1146.                     Oberon.FocusViewer.handle(Oberon.FocusViewer, copyover)
  1147.                 ELSIF (keysum = {rightKey, leftKey}) & F.hasSel THEN Oberon.PassFocus(Viewers.This(F.X, F.Y));
  1148.                     Texts.Delete(F.text, F.selbeg.pos, F.selend.pos); SetCaret(F, F.selbeg.pos)
  1149.                 END
  1150.             ELSE DrawCursor(x, y)
  1151.             END
  1152.         END
  1153.     END Edit;
  1154.     (** General **)
  1155.     PROCEDURE Copy (SF, DF: Frame);
  1156.     BEGIN
  1157.         DF.handle := SF.handle; DF.text := SF.text; DF.org := SF.org;
  1158.         DF.col := SF.col; DF.left := SF.left; DF.right := SF.right; DF.top := SF.top; DF.bot := SF.bot;
  1159.         DF.barW := SF.barW; DF.hasCar := FALSE; DF.hasSel := FALSE; DF.showsParcs := SF.showsParcs;
  1160.         DF.focus := NIL; DF.trailer := NIL
  1161.     END Copy;
  1162.     PROCEDURE Handle* (f: Display.Frame; VAR msg: Display.FrameMsg);
  1163.         VAR F, F1: Frame; pos: LONGINT;
  1164.     BEGIN F := f(Frame);
  1165.         IF msg IS Oberon.InputMsg THEN
  1166.             WITH msg: Oberon.InputMsg DO
  1167.                 IF msg.id = Oberon.consume THEN Write(F, msg.ch, msg.fnt, msg.col, msg.voff)
  1168.                 ELSIF msg.id = Oberon.track THEN Edit(F, msg.X, msg.Y, msg.keys)
  1169.                 END
  1170.             END
  1171.         ELSIF msg IS Oberon.ControlMsg THEN
  1172.             WITH msg: Oberon.ControlMsg DO
  1173.                 IF msg.id = Oberon.defocus THEN RemoveCaret(F)
  1174.                 ELSIF msg.id = Oberon.neutralize THEN
  1175.                     RemoveCaret(F); RemoveSelection(F); PassSubFocus(F, NIL); NotifySubFrames(F, msg)
  1176.                 ELSE NotifySubFrames(F, msg)
  1177.                 END
  1178.             END
  1179.         ELSIF msg IS Oberon.CopyMsg THEN
  1180.             WITH msg: Oberon.CopyMsg DO
  1181.                 IF msg.F = NIL THEN NEW(F1); msg.F := F1 END;
  1182.                 Copy(F, msg.F(Frame))
  1183.             END
  1184.         ELSIF msg IS UpdateMsg THEN NotifySubFrames(F, msg);
  1185.             WITH msg: UpdateMsg DO
  1186.                 IF msg.text = F.text THEN Update(F, msg) END
  1187.             END
  1188.         ELSIF msg IS InsertElemMsg THEN
  1189.             IF F.hasCar THEN pos := F.carloc.pos;
  1190.                 PickAttributes(W, F.text, pos, Oberon.CurFnt, Oberon.CurCol, Oberon.CurOff);
  1191.                 Texts.WriteElem(W, msg(InsertElemMsg).e);
  1192.                 Texts.Insert(F.text, pos, W.buf);
  1193.                 SetCaret(F, pos + 1)
  1194.             END
  1195.         ELSIF msg IS Oberon.SelectionMsg THEN NotifySubFrames(F, msg);
  1196.             WITH msg: Oberon.SelectionMsg DO
  1197.                 IF F.hasSel & (F.time > msg.time) THEN
  1198.                     msg.text := F.text; msg.beg := F.selbeg.pos; msg.end := F.selend.pos; msg.time := F.time
  1199.                 END
  1200.             END
  1201.         ELSIF msg IS Oberon.CopyOverMsg THEN NotifySubFrames(F, msg);
  1202.             WITH msg: Oberon.CopyOverMsg DO
  1203.                 IF F.hasCar THEN Texts.Save(msg.text, msg.beg, msg.end, B);
  1204.                     Texts.Insert(F.text, F.carloc.pos, B); SetCaret(F, F.carloc.pos + (msg.end - msg.beg))
  1205.                 END
  1206.             END
  1207.         ELSIF msg IS MenuViewers.ModifyMsg THEN
  1208.             WITH msg: MenuViewers.ModifyMsg DO
  1209.                 F.handle(F, neutralize); Resize(F, F.X, msg.Y, F.W, msg.H)
  1210.             END
  1211.         ELSIF msg IS SelectMsg THEN NotifySubFrames(F, msg);
  1212.             WITH msg: SelectMsg DO
  1213.                 IF (msg.text = F.text) & ~F.hasSel THEN Oberon.RemoveMarks(F.X, F.Y, F.W, F.H);
  1214.                     F.handle(F, neutralize);
  1215.                     SetSelection(F, msg.beg, msg.end); F.time := msg.time;
  1216.                     IF F.hasSel THEN F.selbeg.pos := msg.beg; F.selend.pos := msg.end END
  1217.                 END
  1218.             END
  1219.         ELSE NotifySubFrames(F, msg)
  1220.         END
  1221.     END Handle;
  1222.     PROCEDURE Open* (F: Frame; T: Texts.Text; pos: LONGINT);
  1223.     BEGIN
  1224.         F.handle := Handle; F.text := T; F.org := pos; F.col := Display.black;
  1225.         F.left := left; F.right := right; F.top := top; F.bot := bot;
  1226.         F.barW := barW; F.hasCar := FALSE; F.hasSel := FALSE; F.showsParcs := FALSE; F.trailer := NIL
  1227.     END Open;
  1228.     PROCEDURE NotifyDisplay* (T: Texts.Text; op: INTEGER; beg, end: LONGINT);
  1229.         VAR msg: UpdateMsg;
  1230.     BEGIN
  1231.         msg.text := T; msg.id := op; msg.beg := beg; msg.end := end; Viewers.Broadcast(msg)
  1232.     END NotifyDisplay;
  1233.     PROCEDURE Text* (name: ARRAY OF CHAR): Texts.Text;
  1234.         VAR text: Texts.Text;
  1235.     BEGIN
  1236.         NEW(text); Texts.Open(text, name); text.notify := NotifyDisplay; RETURN text
  1237.     END Text;
  1238.     PROCEDURE NewText* (T: Texts.Text; pos: LONGINT): Frame;
  1239.         VAR frame: Frame;
  1240.     BEGIN
  1241.         NEW(frame); Open(frame, T, pos);
  1242.         RETURN frame
  1243.     END NewText;
  1244.     PROCEDURE NewMenu* (name, commands: ARRAY OF CHAR): Frame;
  1245.         VAR T, T1: Texts.Text;
  1246.             buf: Texts.Buffer;
  1247.             frame: Frame;
  1248.             fn: ARRAY 32 OF CHAR;
  1249.             i: INTEGER;
  1250.     BEGIN i := 0; T := Text("");
  1251.         Texts.WriteString(W0, name); Texts.WriteString(W0, " | "); Texts.Append(T, W0.buf);
  1252.         IF commands[0] = "^" THEN
  1253.             WHILE commands[i+1] > " " DO fn[i] := commands[i+1]; INC(i) END ;
  1254.             fn[i] := 0X;
  1255.             IF Files.Old(fn) = NIL THEN
  1256.                 IF commands[i+1] = " " THEN INC(i, 2);
  1257.                     WHILE commands[i] > 0X DO Texts.Write(W0, commands[i]); INC(i) END ;
  1258.                     Texts.Append(T, W0.buf)
  1259.                 END
  1260.             ELSE NEW(T1); Texts.Open(T1, fn);
  1261.                 NEW(buf); Texts.OpenBuf(buf); Texts.Save(T1, 0, T1.len, buf); Texts.Append(T, buf)
  1262.             END
  1263.         ELSE Texts.WriteString(W0, commands); Texts.Append(T, W0.buf)
  1264.         END;
  1265.         NEW(frame); Open(frame, T, 0);
  1266.         frame.col := Display.white; frame.left := 6; frame.top := 0; frame.bot := 0; frame.barW := 0;
  1267.         RETURN frame
  1268.     END NewMenu;
  1269. BEGIN
  1270.     Texts.OpenWriter(W); Texts.OpenWriter(W0);
  1271.     Texts.SetFont(W0, Fonts.Default); Texts.SetColor(W0, Display.white); Texts.SetOffset(W0, 0);
  1272.     neutralize.id := Oberon.neutralize;
  1273.     NEW(par);
  1274.     NEW(B); Texts.OpenBuf(B);
  1275.     menuH := Fonts.Default.height + 2;
  1276.     barW := 14; left := barW + 6; right := 8; top := 6; bot := 6;
  1277.     InitDefParc
  1278. END TextFrames.
  1279.